/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.system.domain.SysDeploySystem;
import cn.ibizlab.core.system.service.SysDeploySystemService;
import cn.ibizlab.core.system.filter.SysDeploySystemSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[SysDeploySystem] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysDeploySystemResource {

    @Autowired
    public SysDeploySystemService sysDeploySystemService;

    @Autowired
    @Lazy
    public SysDeploySystemDTOMapping sysDeploySystemDtoMapping;

    @Autowired
    @Lazy
    public SysDeploySystemFilterDTOMapping sysDeploySystemFilterDtoMapping;


    /**
    * 创建Create 系统
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysDeploySystemDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"系统" },  notes = "SysDeploySystem-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Create-all') or hasPermission(this.sysDeploySystemDtoMapping.toDomain(#dto),'uaa-SysDeploySystem-Create')")
    @PostMapping(value = {"sys_deploy_systems","sys_deploy_systems"})
    public ResponseEntity<SysDeploySystemDTO> create
            (@Validated @RequestBody SysDeploySystemDTO dto) {
        SysDeploySystem domain = sysDeploySystemDtoMapping.toDomain(dto);
        sysDeploySystemService.create(domain);
        SysDeploySystem rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysDeploySystemDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 系统
    * 
    *
    * @param id id
    * @return ResponseEntity<SysDeploySystemDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"系统" },  notes = "SysDeploySystem-Get ")
    @PostAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Get-all') or hasPermission(this.sysDeploySystemDtoMapping.toDomain(returnObject.body),'uaa-SysDeploySystem-Get')")
    @GetMapping("sys_deploy_systems/{id}")
    public ResponseEntity<SysDeploySystemDTO> getById
            (@PathVariable("id") String id) {
        SysDeploySystem rt = sysDeploySystemService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysDeploySystemDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 系统
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"系统" },  notes = "SysDeploySystem-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Remove-all') or hasPermission(this.sysDeploySystemService.get(#id),'uaa-SysDeploySystem-Remove')")
    @DeleteMapping("sys_deploy_systems/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysDeploySystemService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 系统
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysDeploySystemDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"系统" },  notes = "SysDeploySystem-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Update-all') or hasPermission(this.sysDeploySystemService.get(#id),'uaa-SysDeploySystem-Update')")
    @VersionCheck(entity = "sysdeploysystem" , versionfield = "updateTime")
    @PutMapping("sys_deploy_systems/{id}")
    public ResponseEntity<SysDeploySystemDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysDeploySystemDTO dto) {
        SysDeploySystem domain = sysDeploySystemDtoMapping.toDomain(dto);
        domain.setId(id);
        sysDeploySystemService.update(domain);
        SysDeploySystem rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysDeploySystemDtoMapping.toDto(rt));
    }

    /**
    * Cache_evict 系统
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysDeploySystemDTO>
    */
    @ApiOperation(value = "Cache_evict", tags = {"系统" },  notes = "SysDeploySystem-Cache_evict ")
    @PostMapping("sys_deploy_systems/cache_evict")
    public ResponseEntity<SysDeploySystemDTO> cacheEvict
            (@Validated @RequestBody SysDeploySystemDTO dto) {
        SysDeploySystem domain = sysDeploySystemDtoMapping.toDomain(dto);
        SysDeploySystem rt = sysDeploySystemService.cacheEvict(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysDeploySystemDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 系统
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"系统" },  notes = "SysDeploySystem-CheckKey ")
    @PostMapping("sys_deploy_systems/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysDeploySystemDTO dto) {
        SysDeploySystem domain = sysDeploySystemDtoMapping.toDomain(dto);
        Integer rt = sysDeploySystemService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * Deploy 系统
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "Deploy", tags = {"系统" },  notes = "SysDeploySystem-Deploy ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Deploy-all') or hasPermission(this.sysDeploySystemDtoMapping.toDomain(#dto),'uaa-SysDeploySystem-Deploy')")
    @PostMapping("sys_deploy_systems/{id}/deploy")
    public ResponseEntity<Boolean> deployById
            (@PathVariable("id") String id, @Validated @RequestBody SysDeploySystemDTO dto) {
        SysDeploySystem domain = sysDeploySystemDtoMapping.toDomain(dto);
        domain.setId(id);
        Boolean rt = sysDeploySystemService.deploy(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * Enabled_System 系统
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysDeploySystemDTO>
    */
    @ApiOperation(value = "Enabled_System", tags = {"系统" },  notes = "SysDeploySystem-Enabled_System ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Enabled_System-all') or hasPermission(this.sysDeploySystemDtoMapping.toDomain(#dto),'uaa-SysDeploySystem-Enabled_System')")
    @PutMapping("sys_deploy_systems/{id}/enabled_system")
    public ResponseEntity<SysDeploySystemDTO> enabledSystemById
            (@PathVariable("id") String id, @Validated @RequestBody SysDeploySystemDTO dto) {
        SysDeploySystem domain = sysDeploySystemDtoMapping.toDomain(dto);
        domain.setId(id);
        SysDeploySystem rt = sysDeploySystemService.enabledSystem(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysDeploySystemDtoMapping.toDto(rt));
    }

    /**
    * 草稿GetDraft 系统
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysDeploySystemDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"系统" },  notes = "SysDeploySystem-GetDraft ")
    @GetMapping("sys_deploy_systems/getdraft")
    public ResponseEntity<SysDeploySystemDTO> getDraft
            (@SpringQueryMap SysDeploySystemDTO dto) {
        SysDeploySystem domain = sysDeploySystemDtoMapping.toDomain(dto);
        SysDeploySystem rt = sysDeploySystemService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysDeploySystemDtoMapping.toDto(rt));
    }

    /**
    * GetPermissions 系统
    * 
    *
    * @param id id
    * @return ResponseEntity<SysDeploySystemDTO>
    */
    @ApiOperation(value = "GetPermissions", tags = {"系统" },  notes = "SysDeploySystem-GetPermissions ")
    @GetMapping("sys_deploy_systems/{id}/getpermissions")
    public ResponseEntity<SysDeploySystemDTO> getPermissionsById
            (@PathVariable("id") String id) {
        SysDeploySystem rt = sysDeploySystemService.getPermissions(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysDeploySystemDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 系统
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysDeploySystemDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"系统" },  notes = "SysDeploySystem-Save ")
    @PostMapping("sys_deploy_systems/save")
    public ResponseEntity<SysDeploySystemDTO> save
            (@Validated @RequestBody SysDeploySystemDTO dto) {
        SysDeploySystem domain = sysDeploySystemDtoMapping.toDomain(dto);
        sysDeploySystemService.save(domain);
        SysDeploySystem rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysDeploySystemDtoMapping.toDto(rt));
    }

    /**
    * All 系统
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysDeploySystemDTO>>
    */
    @ApiOperation(value = "All", tags = {"系统" },  notes = "SysDeploySystem-All ")
    @PostMapping("sys_deploy_systems/all")
    public ResponseEntity<List<SysDeploySystemDTO>> all
            (@Validated @RequestBody SysDeploySystemDTO dto) {
        SysDeploySystem domain = sysDeploySystemDtoMapping.toDomain(dto);
        List<SysDeploySystem> rt = sysDeploySystemService.all(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysDeploySystemDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault 系统
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysDeploySystemDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"系统" },  notes = "SysDeploySystem-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-FetchDefault-all') or hasPermission(#dto,'uaa-SysDeploySystem-FetchDefault')")
    @PostMapping("sys_deploy_systems/fetchdefault")
    public ResponseEntity<List<SysDeploySystemDTO>> fetchDefault
            (@Validated @RequestBody SysDeploySystemFilterDTO dto) {
        SysDeploySystemSearchContext context = sysDeploySystemFilterDtoMapping.toDomain(dto);
        Page<SysDeploySystem> domains = sysDeploySystemService.searchDefault(context) ;
        List<SysDeploySystemDTO> list = sysDeploySystemDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchNav 系统
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysDeploySystemDTO>>
    */
    @ApiOperation(value = "查询FetchNav", tags = {"系统" },  notes = "SysDeploySystem-FetchNav ")
    @PostMapping("sys_deploy_systems/fetchnav")
    public ResponseEntity<List<SysDeploySystemDTO>> fetchNav
            (@Validated @RequestBody SysDeploySystemFilterDTO dto) {
        SysDeploySystemSearchContext context = sysDeploySystemFilterDtoMapping.toDomain(dto);
        Page<SysDeploySystem> domains = sysDeploySystemService.searchNav(context) ;
        List<SysDeploySystemDTO> list = sysDeploySystemDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建系统
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Create-all')")
    @ApiOperation(value = "批量新建系统", tags = {"系统" },  notes = "批量新建系统")
	@PostMapping("sys_deploy_systems/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysDeploySystemDTO> dtos) {
        sysDeploySystemService.createBatch(sysDeploySystemDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除系统
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Remove-all')")
    @ApiOperation(value = "批量删除系统", tags = {"系统" },  notes = "批量删除系统")
	@DeleteMapping("sys_deploy_systems/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysDeploySystemService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新系统
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Update-all')")
    @ApiOperation(value = "批量更新系统", tags = {"系统" },  notes = "批量更新系统")
	@PutMapping("sys_deploy_systems/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysDeploySystemDTO> dtos) {
        sysDeploySystemService.updateBatch(sysDeploySystemDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存系统
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Save-all')")
    @ApiOperation(value = "批量保存系统", tags = {"系统" },  notes = "批量保存系统")
	@PostMapping("sys_deploy_systems/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysDeploySystemDTO> dtos) {
        sysDeploySystemService.saveBatch(sysDeploySystemDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入系统
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysDeploySystem-Save-all')")
    @ApiOperation(value = "批量导入系统", tags = {"系统" },  notes = "批量导入系统")
	@PostMapping("sys_deploy_systems/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysDeploySystemDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysDeploySystemService.importData(config,ignoreError,sysDeploySystemDtoMapping.toDomain(dtos)));
    }

}
